<template>
  <div class="expertDatabase">
    <!--搜索栏-->
    <div class="search_div">
      <el-row :gutter="15" class="expert">
        <el-col :span="6">
          <div class="expert_search">
            <el-select
              v-model="formData.queryProperty"
              placeholder="请选择"
              style="margin-right: 10px"
            >
              <el-option
                v-for="item in selectOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <el-input
              v-model="formData.propertyValue"
              placeholder="请输入查询内容"
              clearable
              prefix-icon="el-icon-search"
            ></el-input>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="expert_search">
            <span class="region_span">区域</span>
            <area_cascader @getOrginId="changeArea" :orgId="formData.organId" />
            <span
              class="close_btn"
              style="margin-left: 12px; margin-top: 1px"
              @click="selectExpertInfo"
              >查询</span
            >
          </div>
        </el-col>
        <!-- <el-col :span="4">
           <el-button  @click="resetInfo">重置</el-button>
        </el-col> -->
        <el-col :span="12">
          <div style="text-align: end">
            <el-dropdown>
              <span class="piliang_operate">
                批量操作<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="importOperate"
                  ><span style="float: left">
                    <svg-icon icon-class="expert_import"></svg-icon>
                  </span>
                  <span style="padding-left: 6px"
                    >批量导入</span
                  ></el-dropdown-item
                >
                <el-dropdown-item @click.native="exportOperate"
                  ><span style="float: left">
                    <svg-icon icon-class="expert_export"></svg-icon>
                  </span>
                  <span style="padding-left: 6px"
                    >批量导出</span
                  ></el-dropdown-item
                >
                <el-dropdown-item @click.native="deleteOperate"
                  ><span style="float: left">
                    <svg-icon icon-class="expert_delete"></svg-icon>
                  </span>
                  <span style="padding-left: 6px; color: red"
                    >批量删除</span
                  ></el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
            <span class="add_span" @click="addExpert">专家入库</span>
          </div>
        </el-col>
      </el-row>
      <!-- <div class="expert hot_area">
        <div>
          热门领域
          <span class="tag_span">
            <span
              v-for="(item, idx) in hot
              Tags"
              :key="idx"
              class=""
              @click="selectTag(item.name)"
              >{{ item.name }}</span
            >
          </span>
        </div>
      </div> -->
      <!-- <el-row class="expert area_row">
        <el-col :span="24">
          <div class="total_p">
            <span class="sort_span"
              ><span>排序规则</span
              ><span :class="this.formData.orderProperty == 1 ? 'selected_style' : ''"
                @click="orderByOperate(param=1)">按姓氏</span
              ><span :class="this.formData.orderProperty == 2 ? 'selected_style' : ''"
              @click="orderByOperate(param=2)">按更新时间</span
              ></span
            >
          </div>
        </el-col>
      </el-row> -->
    </div>
    <el-table
      v-loading="loading"
      element-loading-text="加载中"
      element-loading-spinner="el-icon-loading"
      ref="multipleTable"
      :data="tableData"
      style="width: 100%; margin-top: 20px"
      :default-sort="{ prop: 'createTime', order: 'descending' }"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="80"> </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        sortable
        width="100"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.name || "-" }}
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="联系电话" align="center">
        <template slot-scope="scope">
          {{ scope.row.phone || "-" }}
        </template>
      </el-table-column>
      <el-table-column prop="major" label="专业背景" align="center">
        <template slot-scope="scope">
          {{ scope.row.major || "-" }}
        </template>
      </el-table-column>
      <el-table-column prop="jobTitle" label="职称" align="center">
        <template slot-scope="scope">
          {{ scope.row.jobTitle || "-" }}
        </template>
      </el-table-column>
      <el-table-column prop="studyDirection" label="研究方向" align="center">
        <template slot-scope="scope">
          {{ scope.row.studyDirection || "-" }}
        </template>
      </el-table-column>
      <el-table-column
        prop="areaExpertise"
        label="擅长领域"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ scope.row.areaExpertise || "-" }}
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        sortable
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime) || "-" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="lookAt(scope.row.id)"
            >查看</el-button
          >
          <el-button size="mini" type="text" @click="edit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            style="color: red"
            @click="deleteExperise([scope.row.id])"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      class="hy_page"
      v-show="total > 0"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pageNo"
      :page-size="queryParams.pageSize"
      layout="total, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 批量导入弹出框 -->
    <el-dialog
      title="批量导入"
      :visible.sync="importDialogVisible"
      width="35%"
      class="importDialog"
    >
      <div class="importDialog_class">
        <span>添加Excel表格</span>
        <span style="margin-bottom: 15px"
          >上传文件格式仅支持xls或者xlsx（即Excel格式），且文件大小不得超过10M</span
        >
        <div class="uploadOperate">
          <el-upload
            class="upload-demo"
            :action="action"
            :headers="myHeader"
            :limit="1"
            :file-list="filesList"
            accept=".xls,.xlsx"
            :on-success="handleSucessOperate"
            :on-error="handleErrorOperate"
            :show-file-list="false"
            :before-remove="beforeRemove"
          >
            <span class="btn_class"
              ><i class="el-icon-paperclip"></i>上传文件</span
            >
          </el-upload>
          <div class="btn_class download_style" @click="downloadModule">
            <i class="el-icon-paperclip"></i>下载模板
          </div>
        </div>
      </div>
    </el-dialog>
    <!--文件上传失败弹出框-->
    <el-dialog
      title="导入异常数据"
      :visible.sync="errorDialogVisible"
      width="50%"
      class="errorDialog"
      :show-close="false"
    >
      <el-table :data="errorTableData" border style="width: 100%">
        <el-table-column label="序号" width="100" align="center">
          <template slot-scope="scope">
            {{ Number(scope.$index + 1) }}
          </template>
        </el-table-column>
        <el-table-column prop="errorMessage" label="错误提示" align="center">
        </el-table-column>
        <el-table-column prop="row" label="行号" align="center">
          <template slot-scope="scope">
            第{{ Number(scope.row["rowNo"]) }}行
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <span @click="errorDialogVisible = false" class="close_btn">关闭</span>
      </span>
    </el-dialog>
    <!-- 删除前的确认弹出框 -->
    <transition>
      <el-dialog :visible.sync="deleteDialogVisible" width="30%" title="提示">
        <div class="dialog_body_div">
          <span>您确定要删除选中的成员?</span>
          <span>该操作不可恢复!</span>
          <el-input type="textarea" :rows="2" placeholder="请输入删除理由">
          </el-input>
        </div>
        <span slot="footer" class="dialog-footer">
          <span @click="cancelDelete" class="close_btn">取消</span>
          <span @click="submitDelete" class="close_btn addInvitationStyle"
            >确定</span
          >
        </span>
      </el-dialog>
    </transition>
  </div>
</template>

<script>
import {
  selectExpertInfo,
  getHotAreaExpertiseTag,
  deleteExperise,
} from "@/api/enterprise/expert";
import { getOrganChildren } from "@/api/system/organ";
import { getAccessToken, getTenantId } from "@/utils/auth";
import area_cascader from "../common/area_cascader.vue";
export default {
  name: "expertDatabase",
  components: { area_cascader },
  data() {
    return {
      loading: false,
      filesList: [], //上传文件
      hotTags: [],
      //当前选中的排序规则
      sortRules: 1,
      selectOptions: [
        { label: "专家姓名", value: 1 },
        { label: "专业背景", value: 2 },
        { label: "毕业院校", value: 3 },
        { label: "所在单位", value: 4 },
        { label: "标签信息", value: 5 },
        { label: "擅长领域", value: 6 },
      ],
      formData: {
        propertyValue: "",
        queryProperty: 1,
        organId: [],
        orderProperty: 1, //按照姓氏排序
      },

      organIdProps: {
        checkStrictly: true,
        multiple: false,
        label: "name",
        value: "id",
        lazy: true,
        lazyLoad(node, resolve) {
          const { level } = node;
          if (level == 0) {
            getOrganChildren(0).then((res) => {
              resolve(res.data);
            });
          } else {
            getOrganChildren(node.value).then((res) => {
              let data = res.data.map((item) => {
                if (item.level > 2) {
                  item.leaf = true;
                }
                return item;
              });
              resolve(data);
            });
          }
        },
      },
      // 总条数
      total: 10,
      queryParams: {
        pageNo: 1,
        pageSize: 5,
      },
      tableData: [],
      errorTableData: [
        {
          message: "联系电话格式不正确!,身份证格式不正确!",
          row: "2",
        },
        {
          message: "身份证格式不正确!,联系电话格式不正确!",
          row: "3",
        },
      ],
      multipleSelection: [],
      importDialogVisible: false, //批量导入弹出框
      errorDialogVisible: false, //批量导入失败信息展示弹出框
      deleteDialogVisible: false, //删除弹出框提示
      //文件上传相关参数
      action: `${process.env.VUE_APP_BASE_API}/admin-api/expert/info/expertImport`,
      myHeader: {
        Authorization: "Bearer " + getAccessToken(),
        "tenant-id": getTenantId(),
      },
      deleteIds: [], //删除的id
    };
  },
  created() {
    //获取热门标签数据
    //this.getHotAreaExpertiseTag();
    //获取专辑库分页列表
    this.selectExpertInfo();
  },

  methods: {
    //选择区域
    changeArea(val) {
      this.formData.organId = val;
    },
    //下载模板
    downloadModule() {
      this.downloadFile(
        `${process.env.VUE_APP_BASE_API}/admin-api/expert/info/getExcelTemplate`,
        "专家信息录入模板.xlsx"
      );
    },
    orderByOperate(param) {
      this.formData.orderProperty = param;
      this.selectExpertInfo();
    },
    //上传成功事件
    handleSucessOperate(response, file, fileList) {
      if (response.data.failCount > 0) {
        this.filesList = [];
        this.errorDialogVisible = true;
        this.errorTableData = response.data.errorList;
      } else {
        this.$message.success("导入成功");
        this.importDialogVisible = false;
        this.filesList = [];
        this.selectExpertInfo();
      }
    },
    //上传失败事件
    handleErrorOperate(err, file, fileList) {},
    beforeRemove(file, fileList) {
      this.filesList = [];
    },
    //上传失败
    handleErrorOperate(err, file, fileList) {
      this.errorDialogVisible = true;
    },
    //热门领域点击事件
    selectTag(name) {
      this.formData.queryProperty = 6;
      this.formData.propertyValue = name;
      this.hotTags = this.hotTags.map((item) => {
        if (item.name == name) {
          item.isSelect = true;
        }
        return item;
      });
      console.log("AAA:", this.hotTags);
    },
    getHotAreaExpertiseTag() {
      getHotAreaExpertiseTag().then((res) => {
        this.hotTags = res.data.map((item) => {
          item.isSelect = false;
          return item;
        });
      });
    },
    selectExpertInfo() {
      this.loading = true;
      let params = {
        pageNo: this.queryParams.pageNo,
        pageSize: this.queryParams.pageSize,
        propertyValue: this.formData.propertyValue,
        queryProperty: this.formData.queryProperty,
        organId: this.formData.organId[this.formData.organId.length - 1],
        orderProperty: this.formData.orderProperty,
      };
      selectExpertInfo(params).then((res) => {
        this.loading = false;
        let { data } = res;
        this.tableData = data.list;
        //data.list.length > 0 &&
        // data.list.map((item) => {
        //   item.areaExpertiseContent = item.areaExpertise?item.areaExpertise.split(","):[];
        //   item.tagsContent = item.tags&&item.tags != "" ?item.tags.split(","): [];
        //   return item;
        // });
        this.total = data.total;
      });
    },
    edit(id) {
      //编辑
      this.$router.push({
        path: "/expert/expert_database/add",
        query: { editId: id },
      });
    },
    lookAt(id) {
      //详情查看
      this.$router.push({
        path: "/expert/expert_database/detail",
        query: { editId: id },
      });
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.selectExpertInfo();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNo = val;
      this.selectExpertInfo();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //专家录入
    addExpert() {
      this.$router.push({
        path: "/expert/expert_database/add",
      });
    },
    //批量导入操作
    importOperate() {
      this.importDialogVisible = true;
    },
    //批量导出操作
    exportOperate() {
      let ids =
        this.multipleSelection.length > 0
          ? this.multipleSelection.map((item) => item.id)
          : [];
      // if (ids.length === 0) {
      //   this.$message.warning("未选择专家成员！");
      //   return;
      // }
      //导出接口接入
      let url = `${
        process.env.VUE_APP_BASE_API
      }/admin-api/expert/info/downLoad?orderProperty=${
        this.formData.orderProperty
      }&organId=${
        this.formData.organId[this.formData.organId.length - 1]
      }&propertyValue=${this.formData.propertyValue}&queryProperty=${
        this.formData.queryProperty
      }`;
      this.downloadFile(url, "专家列表.xlsx");
    },
    //批量删除操作
    deleteOperate() {
      let ids =
        this.multipleSelection.length > 0
          ? this.multipleSelection.map((item) => item.id)
          : [];
      if (ids.length === 0) {
        this.$message.warning("未选择专家成员！");
        return;
      }
      this.deleteIds = ids;
      this.deleteDialogVisible = true;
    },
    deleteExperise(ids) {
      this.deleteIds = ids;
      this.deleteDialogVisible = true;
    },
    //取消删除操作
    submitDelete() {
      deleteExperise(this.deleteIds).then((res) => {
        this.deleteDialogVisible = false;
        this.$message.success("删除成功");
        this.selectExpertInfo();
      });
    },
    cancelDelete() {
      this.$refs.multipleTable.clearSelection();
      this.deleteDialogVisible = false;
    },
    //重置
    resetInfo() {
      this.formData.propertyValue = "";
      this.formData.queryProperty = 1;
      this.formData.organId = [];
      this.selectExpertInfo();
    },
  },
};
</script>
<style lang="scss" scoped>
.content_info {
  display: flex;
  .image_info {
    height: 50px;
    width: 50px;
    background: #f2f2f2;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 4%;
    .image {
      width: 44px;
      height: 44px;
      border-radius: 50%;
    }
  }
  .base_info {
    margin-left: 10px;
    p {
      &:nth-child(1) {
        span {
          font-family: SourceHanSansCN-Medium, SourceHanSansCN;
          font-weight: 500;
          &:nth-child(1) {
            font-size: 18px;
            color: #333333;
            line-height: 27px;
          }
          &:nth-child(2) {
            margin-left: 10px;
            background: rgba(72, 119, 238, 0.1);
            border-radius: 4px;
            font-size: 13px;
            color: #4877ee;
            line-height: 20px;
          }
        }
      }
      &:nth-child(2) {
        font-size: 14px;
        font-family: SourceHanSansCN-Normal, SourceHanSansCN;
        font-weight: 400;
        color: #666666;
      }
      &:nth-child(3) {
        font-size: 13px;
        font-family: SourceHanSansCN-Normal, SourceHanSansCN;
        font-weight: 400;
        color: #cccccc;
      }
    }
  }
}
.tag_style {
  display: block;
  max-width: 200px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.hy_page {
  float: right;
  margin-top: 20px;
}
.expertDatabase {
  margin: 10px 20px;
  ::v-deep
    .el-table__header
    .el-table-column--selection
    .cell
    .el-checkbox:after {
    color: #606266;
    content: "全选";
    font-size: 16px;
    margin-left: 10px;
  }
}
.search_div {
  margin-top: 20px;
  .expert {
    margin-bottom: 10px;
  }
  .expert_search {
    display: flex;
    height: 32px;
    .region_span {
      width: 80px;
      line-height: 32px;
      font-size: 14px;
      font-weight: 600;
      color: #999999;
    }
    ::v-deep .el-input__inner {
      height: 33px !important;
      line-height: 33px !important;
    }
  }
  .search_span {
    display: inline-block;
    width: 82px;
    height: 35px;
    border-radius: 3px;
    border: 1px solid #0668f8;
    text-align: center;
    line-height: 35px;
    cursor: pointer;
    font-size: 14px;
    font-family: SourceHanSansCN-Medium, SourceHanSansCN;
    font-weight: 500;
    color: #0668f8;
    margin-right: 10px;
  }
  .hot_area {
    // display: flex;
    height: 40px;
    font-size: 14px;
    font-weight: 400;
    color: #999999;
    div {
      height: 100%;
      text-align: end;
      margin-left: 10px;
      // &:nth-child(1) {
      //   flex: 1;
      //   line-height: 40px;
      // }
      // &:nth-child(1) {
      //   text-align: end;
      //   margin-left: 10px;
      // }
    }
    .tag_span {
      span {
        padding: 5px 15px;
        margin-left: 5px;
        background: #f4f4f5;
        border-radius: 4px;
        cursor: pointer;
      }
      span:hover {
        color: #0668f8;
      }
    }
  }
  .area_row {
    .total_p {
      //border: 1px solid red;
      padding: 9px 0;
      text-align: end;
      font-size: 14px;
      .number_span {
        span {
          padding: 0 3px;
          font-size: 16px;
          font-weight: bold;
          color: #4877ee;
        }
      }
      .sort_span {
        margin-left: 30px;
        span {
          display: inline-block;
          margin-left: 20px;
          cursor: pointer;
        }
        .selected_style {
          font-size: 16px;
          font-weight: bold;
          color: #4877ee;
        }
      }
    }
  }
}
.importDialog {
  .importDialog_class {
    display: flex;
    flex-direction: column;
    span {
      font-size: 16px;
      font-family: SourceHanSansCN-Normal, SourceHanSansCN;
      font-weight: 400;
      color: #666666;
      padding-bottom: 10px;
      &:nth-child(1) {
        font-size: 16px;
        font-weight: 500;
        color: #333333;
      }
      &:nth-child(3) {
        margin-top: 10px;
        cursor: pointer;
      }
    }
  }
}
.btn_class {
  background: linear-gradient(180deg, #ffffff 0%, #f7f7f7 100%);
  box-shadow: 0px 4px 6px 0px rgba(35, 36, 41, 0.1),
    0px 0px 2px 0px rgba(35, 37, 45, 0.27),
    inset 0px 1px 3px 0px rgba(255, 255, 255, 0.5);
  border-radius: 4px;
  padding: 12px 20px;
  margin-right: 20px;
  i {
    margin-right: 5px;
    padding-top: 2px;
    font-size: 20px;
  }
}
.uploadOperate {
  display: flex;
}
.download_style {
  margin-top: -8px;
  padding: 10px 20px !important;
  cursor: pointer;
}
.region_span {
  width: 50px !important;
  font-size: 14px;
  font-weight: normal !important;
  color: #606266 !important;
}
.dialog-header-title {
  .dialog_span {
    font-size: 18px;
    font-family: SourceHanSansCN-Medium, SourceHanSansCN;
    font-weight: 500;
    color: #333333;
  }
}
.dialog_body_div {
  display: flex;
  flex-direction: column;
  // align-items: center;
  // justify-content: center;
  span {
    padding: 10px 0;
    font-size: 14px;
    font-family: SourceHanSansCN-Normal, SourceHanSansCN;
    font-weight: 500;
    color: #666666;
    &:nth-child(2) {
      padding: 0;
      padding-bottom: 20px;
    }
  }
}
::v-deep .el-dialog__body {
  padding-top: 0 !important;
}
::v-deep .el-dialog__title {
  font-weight: bold;
}
.piliang_operate {
  cursor: pointer;
  border-radius: 3px;
  border: 1px solid #4877ee;
  color: #4877ee;
  padding: 7px 15px;
}
.add_span {
  display: inline-block;
  width: 82px;
  height: 34px;
  background: #4877ee;
  border: 1px solid #4877ee;
  border-radius: 3px;
  margin-left: 20px;
  font-size: 14px;
  font-family: SourceHanSansCN-Medium, SourceHanSansCN;
  font-weight: 500;
  color: #ffffff;
  text-align: center;
  line-height: 32px;
  cursor: pointer;
}
.close_btn {
  display: inline-block;
  cursor: pointer;
  width: 82px;
  height: 34px;
  line-height: 32px;
  text-align: center;
  border-radius: 3px;
  font-size: 14px;
  font-family: SourceHanSansCN-Medium, SourceHanSansCN;
  font-weight: 500;
  color: #4877ee;
  border: 1px solid #4877ee;
}
.addInvitationStyle {
  margin-left: 12px;
  color: #ffffff;
  background: #4877ee;
}
</style>
